package com.ds.dsm.view.config.gallery;

import com.ds.common.JDSException;
import com.ds.config.ListResultModel;
import com.ds.enums.db.MethodChinaName;
import com.ds.esd.custom.annotation.CustomAnnotation;
import com.ds.esd.custom.annotation.GridViewAnnotation;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.bean.MethodConfig;
import com.ds.esd.custom.buttonviews.annotation.ButtonViewsAnnotation;
import com.ds.esd.custom.gallery.CustomGalleryViewBean;
import com.ds.esd.custom.gallery.enums.CustomGalleryEvent;
import com.ds.esd.custom.gallery.enums.GalleryRowMenu;
import com.ds.esd.custom.grid.enums.GridMenu;
import com.ds.esd.custom.grid.enums.GridRowMenu;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.dsm.view.ViewEntityConfig;
import com.ds.esd.tool.ui.component.tab.SideBarStatusType;
import com.ds.esd.tool.ui.enums.BarLocationType;
import com.ds.web.util.PageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@RequestMapping("/dsm/view/config/gallery/")
@Controller
@ButtonViewsAnnotation(barLocation = BarLocationType.top, sideBarStatus = SideBarStatusType.expand, barSize = "3em")
@MethodChinaName(cname = "列表配置")
public class GalleryActionGroup {

    @CustomAnnotation(hidden = true, pid = true)
    String viewInstId;

    @CustomAnnotation(hidden = true, pid = true)
    String sourceClassName;


    @CustomAnnotation(hidden = true, uid = true)
    String methodName;

    public GalleryActionGroup() {

    }


    @MethodChinaName(cname = "工具栏")
    @RequestMapping(method = RequestMethod.POST, value = "CustomMenus")
    @GridViewAnnotation(addPath = "GalleryMenuTree")
    @ModuleAnnotation(imageClass = "spafont spa-icon-c-toolbar", caption = "工具栏")
    @APIEventAnnotation(autoRun = true)
    @ResponseBody
    public ListResultModel<List<GalleryMenuView>> getCustomMenus(String sourceClassName, String methodName, String domainId, String viewInstId) {
        ListResultModel<List<GalleryMenuView>> resultModel = new ListResultModel<>();
        try {
            ViewEntityConfig tableConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName, viewInstId);
            MethodConfig methodAPIBean = tableConfig.getSourceConfig().getMethodByName(methodName);
            CustomGalleryViewBean galleryViewBean = (CustomGalleryViewBean) methodAPIBean.getView();
            Set<GridMenu> customMenu = galleryViewBean.getCustomMenu();
            List<GalleryMenuView> views = new ArrayList<>();
            for (GridMenu formMenu : customMenu) {
                views.add(new GalleryMenuView(formMenu, methodAPIBean));
            }
            resultModel = PageUtil.getDefaultPageList(views);

        } catch (JDSException e) {
            e.printStackTrace();
        }
        return resultModel;
    }


    @MethodChinaName(cname = "行按钮组")
    @RequestMapping(method = RequestMethod.POST, value = "RowMenus")
    @GridViewAnnotation(addPath = "RowMenuTree")
    @ModuleAnnotation(imageClass = "spafont spa-icon-c-statusbutton", caption = "行按钮组")
    @APIEventAnnotation(autoRun = true)
    @ResponseBody
    public ListResultModel<List<GalleryMenuView>> getRowMenus(String sourceClassName, String methodName, String domainId, String viewInstId) {
        ListResultModel<List<GalleryMenuView>> resultModel = new ListResultModel<>();
        try {
            ViewEntityConfig tableConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName,  viewInstId);
            MethodConfig methodAPIBean = tableConfig.getSourceConfig().getMethodByName(methodName);
            CustomGalleryViewBean galleryViewBean = (CustomGalleryViewBean) methodAPIBean.getView();
            Set<GalleryRowMenu> rowMenu = galleryViewBean.getRowCmdBean().getRowMenu();

            List<GalleryMenuView> views = new ArrayList<>();
            for (GalleryRowMenu formMenu : rowMenu) {
                views.add(new GalleryMenuView(formMenu, methodAPIBean));
            }
            resultModel = PageUtil.getDefaultPageList(views);

        } catch (JDSException e) {
            e.printStackTrace();
        }

        return resultModel;
    }

    @MethodChinaName(cname = "监听事件")
    @RequestMapping(method = RequestMethod.POST, value = "BindMenus")
    @GridViewAnnotation
    @ModuleAnnotation(imageClass = "spafont spa-icon-c-statusbutton", caption = "监听事件", index = 1)
    @APIEventAnnotation(autoRun = true)
    @ResponseBody
    public ListResultModel<List<GalleryEventView>> getBindMenus(String sourceClassName, String domainId, String viewInstId, String methodName) {
        ListResultModel<List<GalleryEventView>> resultModel = new ListResultModel<>();
        try {
            ViewEntityConfig esdClassConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName, viewInstId);
            MethodConfig apiCallBean = esdClassConfig.getSourceConfig().getMethodByName(methodName);
            CustomGalleryViewBean customGridViewBean = (CustomGalleryViewBean) apiCallBean.getView();
            Set<CustomGalleryEvent> events = customGridViewBean.getEvent();
            List<GalleryEventView> views = new ArrayList<>();
            for (CustomGalleryEvent CustomGalleryEvent : events) {
                views.add(new GalleryEventView(CustomGalleryEvent, apiCallBean));
            }
            resultModel = PageUtil.getDefaultPageList(views);
        } catch (JDSException e) {
            e.printStackTrace();
        }
        return resultModel;
    }

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public String getViewInstId() {
        return viewInstId;
    }

    public void setViewInstId(String viewInstId) {
        this.viewInstId = viewInstId;
    }

    public String getSourceClassName() {
        return sourceClassName;
    }

    public void setSourceClassName(String sourceClassName) {
        this.sourceClassName = sourceClassName;
    }
}
